package service;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;
import Dao.RegisterDao;
import bean.LoginResult;
import bean.RegisterResult;

/**
 * Servlet implementation class RegisterService
 */
public class RegisterService extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterService() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		RegisterDao registerDao  = new RegisterDao();
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter writer = response.getWriter();
		String userName = request.getParameter("userName");
		String userpwd = request.getParameter("userPwd");
		String usertype = request.getParameter("userType");
		List<Object> params = new ArrayList<Object>();
		params.add(userName);
		params.add(userpwd);
		params.add(usertype);
		RegisterResult registerResult =registerDao.startRegister(params);
		try {
			if(registerResult.getResultCode()==1)
			{
				request.getSession().setAttribute("typeCode", "hah");
			}
			JSONObject json = JSONObject.fromObject(registerResult);
			writer.println(json.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
		}
		writer.flush();
		writer.close();
	}

}
